import { _decorator, Component, BoxCollider2D, Contact2DType,PolygonCollider2D } from 'cc';
const { ccclass, property } = _decorator;
import { Monster } from './monsterData';
import { Player } from './playerData';
import { createMonster } from './createMonster';

@ccclass('TouchEvent')
export class TouchEvent extends Component {

  del = new createMonster()
  player = new Player()
  start() {

    // 注册单个碰撞体的回调函数
    let collider = this.getComponent(PolygonCollider2D);
    if (collider) {

      collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

    }
  }

  update(deltaTime: number) {

  }
  onBeginContact(self, other) {
    
    
    let player = other.node.parent.getComponent(Player);
    let monster = self.node.getComponent(Monster)
    monster.health -= player.atk;
    if (monster.health <= 0) {
      monster.monsterDie();
      
      
      self.node.destroy();
      return;
    }
  }
}

